package eu.jpereira.trainings.designpatterns.creational.builder;

import java.util.Iterator;

import eu.jpereira.trainings.designpatterns.creational.builder.model.ReportBody;
import eu.jpereira.trainings.designpatterns.creational.builder.model.SaleEntry;
import eu.jpereira.trainings.designpatterns.creational.builder.model.SoldItem;

public class HTMLBuilder implements BodyBuilder {

	private SaleEntry saleEntry;
	private HTMLReportBody body;
	
	@Override
	public void construct(SaleEntry saleEntry) {
		// TODO Auto-generated method stub
		this.saleEntry = saleEntry;
		body = new HTMLReportBody();

	}

	@Override
	public void addCustomerInfo() {
		// TODO Auto-generated method stub
		body.putContent("<span class=\"customerName\">");
        body.putContent(this.saleEntry.getCustomer().getName());
        body.putContent("</span><span class=\"customerPhone\">");
        body.putContent(this.saleEntry.getCustomer().getPhone());
        body.putContent("</span>");

	}

	@Override
	public void addItems() {
		// TODO Auto-generated method stub
		body.putContent("<items>");

        Iterator<SoldItem> it = saleEntry.getSoldItems().iterator();
        while ( it.hasNext() ) {
            SoldItem soldEntry= it.next();
            body.putContent("<item><name>");
            body.putContent(soldEntry.getName());
            body.putContent("</name><quantity>");
            body.putContent(soldEntry.getQuantity());
            body.putContent("</quantity><price>");
            body.putContent(soldEntry.getUnitPrice());
            body.putContent("</price></item>");
        }
        body.putContent("</items>");
        
	}

	@Override
	public ReportBody getBody() {
		// TODO Auto-generated method stub
		return this.body;
	}

}
